package main

import (
	"fmt"
	"github.com/julienschmidt/httprouter"
	"io"
	"log"
	"net/http"
	"os"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
	fmt.Printf("===header: \n")
	for k, v := range r.Header {
		fmt.Printf("%v: %v\n", k, v)
	}
	fmt.Printf("===request:\n")
	fmt.Println(r.Form.Encode())
	fmt.Printf("===body:\n")
	io.Copy(os.Stdout, r.Body)

}

func Post(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	fmt.Printf("===header: \n")
	for k, v := range r.Header {
		fmt.Printf("%v: %v\n", k, v)
	}

	fmt.Printf("===response:\n")

	fmt.Printf("===body:\n")
	io.Copy(os.Stdout, r.Body)
	fmt.Fprintf(w, "hello %s\n", ps.ByName("name"))
}
func pac(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	var ss []int
	_ = ss[1]
}

func main() {
	router := httprouter.New()
	router.GET("/", Index)
	router.GET("/hello/:name", Hello)
	router.PanicHandler = func(w http.ResponseWriter, r *http.Request, i interface{}) {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, "errpr: %v", i)
	}
	router.GET("/pac", pac)
	router.POST("/post/:name", Post)
	log.Fatal(http.ListenAndServe(":8080", router))
}
